# 设置正确的Java路径
$env:JAVA_HOME = "C:\Program Files\Java\jdk-21"

# 查找Maven仓库中的Spring和相关依赖
$m2Repo = "$HOME\.m2\repository"
$dependencies = Get-ChildItem -Path $m2Repo -Recurse -Filter "*.jar" | Where-Object { 
    $_.FullName -match 'springframework|spring-boot|jackson|log4j|commons-io|pdfbox|tomcat' 
} | Select-Object -ExpandProperty FullName

# 创建target目录
if(!(Test-Path "target\classes")){
    New-Item -ItemType Directory -Force -Path "target\classes"
}

# 编译Java源代码
Write-Host "正在编译Java源代码..."
try {
    $proc = Start-Process -FilePath "$env:JAVA_HOME\bin\javac.exe" `
                         -ArgumentList "-encoding", "UTF-8", "-cp", ($dependencies -join ";"), "-d", "target\classes", "src\main\java\com\example\deepseek\backend\DeepSeekBackendApplication.java" `
                         -Wait -NoNewWindow -PassThru
    
    if ($proc.ExitCode -ne 0) {
        Write-Host "编译失败，退出码: $($proc.ExitCode)" -ForegroundColor Red
        pause
        exit 1
    }
    
    # 运行应用程序
    Write-Host "编译成功，正在启动应用程序..." -ForegroundColor Green
    Start-Process -FilePath "$env:JAVA_HOME\bin\java.exe" `
                 -ArgumentList "-cp", "$($dependencies -join ';');target\classes", "com.example.deepseek.backend.DeepSeekBackendApplication" `
                 -NoNewWindow
} catch {
    Write-Host "发生错误: $_" -ForegroundColor Red
    pause
}